#ifndef _COUNTRIES_H_
#define _COUNTRIES_H_


#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "Resource.h"
#include "Vector3.h"
#include "Effects.h"

class ItemPtr;
class EffectPtr;
class CharPtr;
class CreatureAIScript;

//物品公共属性
typedef struct country_common_property
{

	ResourceProxy		uplink;		//地图 Region 对象
	void initialize(void)
	{
		
		uplink.setNull();
	}

	country_common_property()
	{
		initialize();
	}

}country_common_property_t;

class  Country:public Resource
{
public:
	enum CountryStatus
	{
		CountryStatusNone		= 0,	//无效状态
		CountryStatusNormal		= 1,	//一般状态
		CountryStatusFreeze		= 2,	//冻结状态
		CountryStatusLordChange		= 3,	//君主更换
		CountryStatusDeleted		= 255	//删除状态
	};

	Country(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
	~Country(void);

protected:
       	void prepareImpl(void);
        /** Destroys data cached by prepareImpl.
         */
        void unprepareImpl(void);
        /// @copydoc Resource::loadImpl
        bool loadImpl(void);
        /// @copydoc Resource::postLoadImpl
        void postLoadImpl(void);

	 void preUnloadImpl(void);
        /// @copydoc Resource::unloadImpl
        void unloadImpl(void);
        /// @copydoc Resource::calculateSize
        size_t calculateSize(void) const;
	//Resource::loadChildrenFromDB
	bool loadChildrenFromDB(void);

private:
	std::list<ResourceProxy>        m_chars;        //国家角色数量
	std::list<ResourceProxy>	m_items;	//包含的物品序列号
	std::list<ResourceProxy>	m_effects;	//国家作用的效果
	
	uint32		m_serial;
	
	String		m_country_name;

	void    initialize(void);
	//载入物品
	size_t loadItemsFromDB(void);
	//载入效果
	size_t loadEffectsFromDB(void);	
public:

	const uint32  & getSerial() 	{	return m_serial;	}

	//国家只自动加入效果，物品子对象
	bool hasChildren(void)  {       return true;    }

	//添加释放国家角色
	bool	addChar(const uint32 & serial);
	bool  	addChar(ResourceProxy & ch);
	bool	removeChar(const uint32 & serial);
	bool	removeChar(ResourceProxy  & ch);
	bool	deleteChar(const uint32 & serial);
	bool	deleteChar(ResourceProxy & ch);
	bool	destroyChar(const uint32 & serial);
	bool	destroyChar(ResourceProxy & ch);
	void	removeAllChars(void);
	void	destroyAllChars(void);
	void	deleteAllChars(void);
	
	//添加释放国家包含的物品
	bool	addItem(const uint32 & serial);
	bool  	addItem(ResourceProxy & item);
	bool	removeItem(const uint32 & serial);
	bool	removeItem(ResourceProxy & item);
	bool	deleteItem(const uint32 & serial);
	bool	deleteItem(ResourceProxy & item);
	bool	destroyItem(const uint32 & serial);
	bool	destroyItem(ResourceProxy & item);
	void	removeAllItems(void);
	void	destroyAllItems(void);
	void	deleteAllItems(void);

	//添加效果到角色
	bool	addEffect(const uint32 & serial);
	bool	addEffect(ResourceProxy & effect);
	bool	removeEffect(const uint32 & serial);
	bool	removeEffect(ResourceProxy & effect);
	bool	deleteEffect(const uint32 & serial);
	bool	deleteEffect(ResourceProxy & effect);
	bool	destroyEffect(const uint32 & serial);
	bool	destroyEffect(ResourceProxy & effect);
	void	removeAllEffects(void);
	void	destroyAllEffects(void); 
	void	deleteAllEffects(void); 
	
	//得到包括特效的列表复制
	std::list<EffectPtr> * getEffectsList(std::list<EffectPtr> * list);
	//得到包含物品的列表复制
	std::list<ItemPtr> * getItemsList(std::list<ItemPtr> * list);
	//得到角色列表
        std::list<CharPtr> * getCharsList(std::list<CharPtr> * list);	

	const String & getCountryName(void);

	const CountryStatus  getCountryStatus(void);
	void setCountryStatus(const CountryStatus & status);
	//save item to database	
	bool	SaveDB(void);
	bool	LoadDB(void);
	bool    FetchDB(Field * fields);

	void  Delete(void);
	void  Destroy(void);

	//国家公共属性
	country_common_property_t	common;
};

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to CountryPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class SERVER_DECL  CountryPtr : public SharedPtr<Country>
    {
    public:
        CountryPtr() : SharedPtr<Country>() {}
        explicit CountryPtr(Country* rep) : SharedPtr<Country>(rep) {}
        CountryPtr(const CountryPtr& r) : SharedPtr<Country>(r) {}
        CountryPtr(const ResourcePtr& r);
        /// Operator used to convert a ResourcePtr to a MeshPtr
        CountryPtr& operator=(const ResourcePtr& r);
    protected:
        /// Override destroy since we need to delete Mesh after fully defined
        void destroy(void);
    };
	

#endif
